import {
    fetchUserCenter
} from '../../services/usercenter/fetchUsercenter';
import {
    getToPayOrderCount,
    getToSendOrderCount,
    getToReceiveOrderCount
} from '../../services/order/order';
import {
    ORDER_STATUS
} from '../../services/order/order';
import Toast from 'tdesign-miniprogram/toast/index';

const menuData = [
    [{
            title: '我的订单',
            tit: '',
            url: '/pages/order/order-list/index',
            type: 'order',
        },
        {
            title: '我的优惠券',
            tit: '',
            url: '',
            type: '',
        },
        {
            title: '我的积分',
            tit: '',
            url: '',
            type: '',
        },
        {
            title: '手机认证',
            tit: '',
            url: '',
            type: '',
        },
        {
            title: '客服中心',
            tit: '',
            url: '',
            type: '',
        },
        {
            title: '投诉建议',
            tit: '',
            url: '',
            type: '',
        },
        {
            title: '设置中心',
            tit: '',
            url: '',
            type: '',
        }
    ],
];

const orderTagInfos = [{
        title: '待预约',
        iconName: 'wallet',
        orderNum: 0,
        tabType: ORDER_STATUS.TO_PAY,
        status: 1,
    },
    {
        title: '待上课',
        iconName: 'deliver',
        orderNum: 0,
        tabType: ORDER_STATUS.TO_SEND,
        status: 1,
    },
    {
        title: '未上课',
        iconName: 'package',
        orderNum: 0,
        tabType: ORDER_STATUS.TO_RECEIVE,
        status: 1,
    },
];

const getDefaultData = () => ({
    showMakePhone: false,
    userInfo: {
        avatarUrl: '',
        nickName: '正在登录...',
        phoneNumber: '',
    },
    menuData,
    orderTagInfos,
    customerServiceInfo: {},
    currAuthStep: 1,
    showKefu: true,
    versionNo: '',
    toPayOrderCount: 0,
    toSendOrderCount: 0,
    toReceiveOrderCount: 0,
});

Page({
    data: getDefaultData(),

    onLoad() {
        this.getVersionInfo();
    },

    onShow() {
        this.init();
    },
    onPullDownRefresh() {
        this.init();
    },

    init() {
        this.fetUseriInfoHandle();
        this.initOrderCount();
    },

    async initOrderCount() {
        const [pay, send, receive] = await Promise.all([
            getToPayOrderCount(),
            getToSendOrderCount(),
            getToReceiveOrderCount(),
        ]);
        this.setData({
            'orderTagInfos[0].orderNum': pay,
            'orderTagInfos[1].orderNum': send,
            'orderTagInfos[2].orderNum': receive,
        });
    },

    fetUseriInfoHandle() {
        fetchUserCenter().then((res) => {
            // eslint-disable-next-line no-unused-expressions
            this.setData({
                userInfo: res.data,
                currAuthStep: 2,
            });
            wx.stopPullDownRefresh();
        });
    },

    onClickCell({
        currentTarget
    }) {
        const {
            type
        } = currentTarget.dataset;

        switch (type) {
            case 'order': {
                wx.navigateTo({
                    url: '/pages/order/order-list/index'
                });
                break;
            }
            case 'service': {
                this.openMakePhone();
                break;
            }
            case 'help-center': {
                Toast({
                    context: this,
                    selector: '#t-toast',
                    message: '你点击了帮助中心',
                    icon: '',
                    duration: 1000,
                });
                break;
            }
            case 'point': {
                Toast({
                    context: this,
                    selector: '#t-toast',
                    message: '你点击了积分菜单',
                    icon: '',
                    duration: 1000,
                });
                break;
            }
            case 'coupon': {
                wx.navigateTo({
                    url: '/pages/coupon/coupon-list/index'
                });
                break;
            }
            default: {
                Toast({
                    context: this,
                    selector: '#t-toast',
                    message: '未知跳转',
                    icon: '',
                    duration: 1000,
                });
                break;
            }
        }
    },

    jumpNav(e) {
        const status = e.detail.tabType;

        if (status === 0) {
            wx.navigateTo({
                url: '/pages/course/index'
            });
        } else {
            wx.navigateTo({
                url: `/pages/course/index?status=${status}`
            });
        }
    },

    jumpAllOrder() {
        wx.navigateTo({
            url: '/pages/course/index'
        });
    },

    openMakePhone() {
        this.setData({
            showMakePhone: true
        });
    },

    closeMakePhone() {
        this.setData({
            showMakePhone: false
        });
    },

    call() {
        wx.makePhoneCall({
            phoneNumber: this.data.customerServiceInfo.servicePhone,
        });
    },

    gotoUserEditPage() {
        const {
            currAuthStep
        } = this.data;
        if (currAuthStep === 2) {
            wx.navigateTo({
                url: '/pages/usercenter/person-info/index'
            });
        } else {
            this.fetUseriInfoHandle();
        }
    },

    getVersionInfo() {
        const versionInfo = wx.getAccountInfoSync();
        const {
            version,
            envVersion = __wxConfig
        } = versionInfo.miniProgram;
        this.setData({
            versionNo: envVersion === 'release' ? version : envVersion,
        });
    },
});